package com.org.manhu.chargehelper.tools;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.org.manhu.chargehelper.ChargeApplication;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.net.Uri;

/**
 * @文件名：ApkInfoUtils.java
 * @包名：com.org.manhu.tool
 * @作者：李朋辉 @QQ：892839211
 * @创建时间：2014年12月25日 上午10:15:01、
 *
 * @描述：
 */
public class ApkInfoUtils {

	private static PackageManager pm = ChargeApplication.mApplicationContext
			.getPackageManager();

	/**
	 * 获取apk包的信息：版本号，名称，图标等
	 * 
	 * @param absPath
	 *            apk包的绝对路径
	 * @param context
	 */
	public static ApplicationInfo getApkInfo(String absPath, Context context) {
		Map<String, Object> apkInfo = new HashMap<String, Object>();
		ApplicationInfo appInfo = null;
		PackageInfo pkgInfo = pm.getPackageArchiveInfo(absPath,
				PackageManager.GET_ACTIVITIES);
		if (pkgInfo != null) {
			appInfo = pkgInfo.applicationInfo;
			/* 必须加这两句，不然下面icon获取是default icon而不是应用包的icon */
			appInfo.sourceDir = absPath;
			appInfo.publicSourceDir = absPath;
			String appName = pm.getApplicationLabel(appInfo).toString();// 得到应用名
			String packageName = appInfo.packageName; // 得到包名
			// String version = pkgInfo.versionName; // 得到版本信息
			/* icon1和icon2其实是一样的 */
			Drawable icon1 = pm.getApplicationIcon(appInfo);// 得到图标信息
			// Drawable icon2 = appInfo.loadIcon(pm);
			// String pkgInfoStr = String.format(
			// "PackageName:%s, Vesion: %s, AppName: %s", packageName,
			// version, appName);
			// Log.i("TAG", String.format("PkgInfo: %s", pkgInfoStr));
			apkInfo.put("appName", appName);
			apkInfo.put("packageName", packageName);
			apkInfo.put("icon1", icon1);
		}

		return appInfo;
	}

	/**
	 * @描述：
	 * @param context
	 * @param packageName
	 * @return
	 */
	public static int getVersionCode(/* Context context, */String packageName) {
		int versionCode = -1;

		try {
			PackageInfo info = ChargeApplication.mApplicationContext
					.getPackageManager().getPackageInfo(packageName, 0);
			versionCode = info.versionCode;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return versionCode;
	}

	/**
	 * @描述：
	 * @param context
	 * @param packageName
	 * @return
	 */
	public static String getVersionName() {
		String versionName = "";

		try {
			PackageInfo info = ChargeApplication.mApplicationContext
					.getPackageManager()
					.getPackageInfo(
							ChargeApplication.mApplicationContext
									.getPackageName(),
							0);
			versionName = info.versionName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return versionName;
	}

	/**
	 * 通过包名获取应用程序的名称。
	 * 
	 * @param context
	 *            Context对象。
	 * @param packageName
	 *            包名。
	 * @return 返回包名所对应的应用程序的名称。
	 */
	public static String getAppNameByPackageName(Context context,
			String packageName) {
		PackageManager pm = context.getPackageManager();
		String name = null;
		try {
			name = pm.getApplicationLabel(
					pm.getApplicationInfo(packageName,
							PackageManager.GET_META_DATA)).toString();
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return name;
	}

	/**
	 * 判断应用是否已安装
	 * 
	 * @param context
	 * @param packageName
	 * @return
	 */
	public static boolean isInstalled(Context context, String packageName) {
		boolean hasInstalled = false;
		List<PackageInfo> list = pm
				.getInstalledPackages(PackageManager.PERMISSION_GRANTED);
		for (PackageInfo p : list) {
			if (packageName != null && packageName.equals(p.packageName)) {
				hasInstalled = true;
				break;
			}
		}
		return hasInstalled;
	}

	public static boolean isInstalled(String packageName) {
		boolean isInstall = false;
		PackageInfo packageInfo = null;
		try {
			packageInfo = ChargeApplication.mApplicationContext
					.getPackageManager().getPackageInfo(packageName, 0);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		if (packageInfo == null) {
			isInstall = false;
		} else {
			isInstall = true;
		}

		return isInstall;
	}

	/**
	 * @描述：打开其他应用
	 * @param context
	 */
	public static void openAppByPath(Context context, String path) {
		Intent intent = new Intent();
		intent = pm
				.getLaunchIntentForPackage(getApkInfo(path, context).packageName);
		context.startActivity(intent);
	}

	/**
	 * @描述：打开其他应用
	 * @param context
	 */
	public static void openAppByPackageName(Context context, String packageName) {
		Intent intent = new Intent();
		intent = pm.getLaunchIntentForPackage(packageName);
		context.startActivity(intent);
	}

	/**
	 * @描述：安装应用
	 * @param context
	 * @param path
	 *            apk的路径
	 */
	public static void install(Context context, String path) {
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_VIEW);
		File file = new File(path);
		intent.setDataAndType(Uri.fromFile(file),
				"application/vnd.android.package-archive");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}

	public static void installNoUi(Context context, String path) {
		Intent install_hide_intent = new Intent(
				"android.intent.action.VIEW.HIDE");
		install_hide_intent.setDataAndType(Uri.parse(path),
				"application/vnd.android.package-archive");
		context.startActivity(install_hide_intent);
	}

	/**
	 * @描述：获取下载文件夹中的所有文件的路径
	 * @param path
	 *            下载路径所在的文件夹
	 * @return
	 */
	public static List<String> getFileList(String path, Context context) {
		List<String> list = new ArrayList<String>();
		File file = new File(path);
		if (file.exists()) {
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (getApkInfo(files[i].getAbsolutePath(), context) != null) {
					list.add(files[i].getAbsolutePath());
				}
			}
		}
		return list;

	}

}
